package com.heima.user.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.heima.common.dto.ResponseResult;
import com.heima.common.enums.AppHttpCodeEnum;
import com.heima.common.util.AppJwtUtil;
import com.heima.user.dto.LoginDto;
import com.heima.user.dto.WmUser;
import com.heima.user.entity.ApUser;
import com.heima.user.mapper.ApUserMapper;
import com.heima.user.service.IApUserService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;
import org.springframework.util.DigestUtils;

import java.util.HashMap;
import java.util.Map;

/**
 * <p>
 * APP用户信息表 服务实现类
 * </p>
 *
 * @author zsk
 * @since 2022-04-11
 */
@Service
public class ApUserServiceImpl extends ServiceImpl<ApUserMapper, ApUser> implements IApUserService {

    /**
     * 登录验证
     *
     * @param dto
     * @return
     */
    @Override
    public ResponseResult login(LoginDto dto) {

        if (dto == null ||
                (StringUtils.isEmpty(dto.getPhone()) && StringUtils.isEmpty(dto.getPassword())
                        && StringUtils.isEmpty(dto.getEquipmentId()))) {
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID);
        }

        //判断是否包含手机号和密码
        if (!StringUtils.isEmpty(dto.getPhone()) && !StringUtils.isEmpty(dto.getPassword())) {
            //构建查询条件

            LambdaQueryWrapper<ApUser> wrapper = new LambdaQueryWrapper<>();
            wrapper.eq(ApUser::getPhone, dto.getPhone());

            //查询
            ApUser one = this.getOne(wrapper);
            if (one == null) {
                return ResponseResult.errorResult(AppHttpCodeEnum.DATA_NOT_EXIST);
            }

            //获取用户盐
            String pwd = dto.getPassword() +one.getSalt();
            //使用md5加密
            String pass = DigestUtils.md5DigestAsHex(pwd.getBytes());

            //与数据库查询到的数据进行比对
            if (pass.equals(one.getPassword())) {
                //一致，生成token返回数据
                Map<String, Object> map = new HashMap<>();
                map.put("userId", one.getId());
                String token = AppJwtUtil.getToken(map);
                Map<String, Object> result = new HashMap<>();
                result.put("token", token);
                //返回
                return ResponseResult.okResult(result);
            } else {
                //返回密码错误
                return ResponseResult.errorResult(AppHttpCodeEnum.LOGIN_PASSWORD_ERROR);
            }
        }

        //如果是游客登录，设备号不为空
        if (!StringUtils.isEmpty(dto.getEquipmentId())) {
            //生成token
            Map<String, Object> map = new HashMap<>();
            map.put("userId", 0);//游客登录存入0
            String token = AppJwtUtil.getToken(map);
            Map<String, Object> result = new HashMap<>();
            result.put("token", token);

            //返回
            return ResponseResult.okResult(result);
        }
        //其他情况返回null
        return null;
    }
}
